package com.hoshiicloud.user.controller.rbac;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.rbac.entity.SysOrganization;
import com.hoshiicloud.rbac.vo.SysOrganizationDetailVO;
import com.hoshiicloud.rbac.vo.SysOrganizationVO;

import com.hoshiicloud.user.param.rbac.SysOrganizationInsertParam;
import com.hoshiicloud.user.param.rbac.SysOrganizationQueryParam;
import com.hoshiicloud.user.param.rbac.SysOrganizationUpdateParam;
import com.hoshiicloud.user.service.rbac.ISysOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门控制器
 *
 * @author Mr.wu
 *
 */
@Api(tags = "业务部门")
@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController {

	@Autowired
	private ISysOrganizationService SysOrganizationService;

	@ApiOperation("新增部门")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addDept(@Valid @RequestBody SysOrganizationInsertParam param) {
		SysOrganization dept = new SysOrganization();
		BeanUtils.copyProperties(param, dept);
		// TODO 只接受指定参数
		return SysOrganizationService.addDept(dept);
	}

	@ApiOperation("修改部门")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateDept(@Valid @RequestBody SysOrganizationUpdateParam param) {
		SysOrganization dept = new SysOrganization();
		BeanUtils.copyProperties(param, dept);
		dept.setModifiedTime(LocalDateTime.now());
		return SysOrganizationService.updateDept(dept);
	}

	@ApiOperation("删除部门")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteDept(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return SysOrganizationService.deleteDept(id);
	}

	@ApiOperation("获取部门详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysOrganizationDetailVO> getDeptDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return SysOrganizationService.getDetail(id);
	}

	@ApiOperation("分页查询部门")
	@RequestMapping(value = "/getPage", method = RequestMethod.POST)
	public Response<IPage<SysOrganization>> getDeptPage(@Valid @RequestBody SysOrganizationQueryParam param) throws Exception {
		QueryWrapper<SysOrganization> queryWrapper = param.convertToQueryWrapper();
		IPage<SysOrganization> page = new Page<>(Long.parseLong(param.getPageNumber()),
				Long.parseLong(param.getPageSize()));
		String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
		return Response.data(SysOrganizationService.getPage(page,queryWrapper,key));
	}

}
